/*
*******************************************************************************
*                              (c) Copyright HighSun Techonology Company
*                                           All Rights Reserved
*
* File    : app.c
* By      : HuangXi
* Version : V0.1
* Datetime: 2012-03-09 AM 10:35
* Description: Application entry
*******************************************************************************
*/

#include <includes.h>
#include <math.h>


/* Application entry */
void main(void)
{
/*
    INT8U  c   = 0x0;
    INT8U  err = 0x0;
    Uint32 i   = 0;
    char *msg  = (char *)0;
*/  
    INT8U str[30] = {0};
    Uint32  tmp1  = 0;
    
    /* Disable the WatchDog, setup the PLL and peripheral clocks */
    InitSysCtrl();

    /* Initialize the PIE block */
    InitPieBlock();

    /* Copy the InitFlash function to RAM */
    MemCopy(&RamfuncsLoadStart, &RamfuncsLoadEnd, &RamfuncsRunStart);

    /* Initialize the Flash pipe */
    InitFlash();

    /* Initialize the Timer0 */
    InitCpuTimers(); 

    configtestled();
   
    /* Start timer0 */
   // StartCpuTimer0();
   // StartCpuTimer1();
   // StartCpuTimer2();

    /* Enable Global interrupt INTM*/
    EINT;   
	
    /* Enable Global realtime interrupt DBGM*/
    ERTM;  
	


    //CommInit();

    /* Initialize the SCI */
    //init_sci();

    
    InitScicGpio();
    scic_debug_init();      
    scic_para_init();  

    /* Init ADC */
    init_adc();

    //debug_print("%s%s","XLS-600 Excitation regulator","\n\r");

    /* Init CAP1 for frequency calculation */
    cap1_init();


    debug_print("%s%s","*****HighSun Technology*****","\n\r");
    debug_print("%s%s","XLS-600 AVR ","\n\r");
    debug_print("%s%s","AD sample successfully on 2012-04-06","\n\r");
    
    while (1)
    {   

        //debug_print("%s%d%s","f:",get_freq(),"\n\r");

/*
       c = CommGetChar(COMM1, &err);

       if (COMM_RX_EMPTY != err)
       {
           CommPutChar(COMM1, c);
       }
*/
       DELAY_US(50000);

       if (get_prev_freq_cal() != get_freq_cal())
       {
           debug_print("%s%ld%s","f*1000:",   get_freq(),      "\n\r");
           debug_print("%s%ld%s","Uab:", get_vol_Uab(),   "\n\r");
           debug_print("%s%ld%s","Ucb:", get_vol_Ucb(),   "\n\r");
           debug_print("%s%ld%s","Ia*100:",  get_cur_Ia(),    "\n\r");
           debug_print("%s%ld%s","Ic*100:",  get_cur_Ic(),    "\n\r");
           debug_print("%s%ld%s","IL*100:",  get_il(),    "\n\r");
           debug_print("%s%ld%s","UL*100:",  get_ul(),    "\n\r");

          #if 0
           debug_print("%s%ld%s","P*1000:",   get_P(),    "\n\r");
           debug_print("%s%ld%s","Q*1000:",   get_Q(),    "\n\r");
           debug_print("%s%ld%s","S*1000:",    get_S(),    "\n\r");
           debug_print("%s%ld%s","cos*100:", get_cos(),    "\n\r");
          #endif
#if 0
           tmp1 = get_freq();
           sprintf((char*)&str[0],"f=%ld",  tmp1);
	       disp8_16ASCIIStr(0,5,(INT8U*)&str[0]);

           tmp1 = get_vol_Uab();
           sprintf((char*)&str[0],"Uab = %ld", tmp1);
	       disp8_16ASCIIStr(0,20,(INT8U*)&str[0]);

           tmp1 = get_vol_Ucb();
           sprintf((char*)&str[0],"Ucb = %ld", tmp1);
	       disp8_16ASCIIStr(0,35,(INT8U*)&str[0]);

           tmp1 = get_cur_Ia();
           sprintf((char*)&str[0],"Ia = %ld", tmp1);
	       disp8_16ASCIIStr(0,50,(INT8U*)&str[0]);

           tmp1 = get_cur_Ic();
           sprintf((char*)&str[0],"Ic = %ld", tmp1);
	       disp8_16ASCIIStr(0,65,(INT8U*)&str[0]);
         #endif
         
           set_prev_freq_cal(get_freq_cal());
       }else{
           debug_print("%s%d%s","f:",   0, "\n\r");
           debug_print("%s%d%s","Uab:", 0, "\n\r");
           debug_print("%s%d%s","Ucb:", 0, "\n\r");
           debug_print("%s%d%s","Ia:",  0, "\n\r");
           debug_print("%s%d%s","Ic:",  0, "\n\r");
       }

        
        #if (PRINT_POINTS == 1)
            print_points();
        #endif

    }

}




